#include <stdio.h>
#include <string.h>

#include "comm.h"
#include "defines.h"
#include "draw_staff.h"

int main(int argc, char *argv[])
{
	char filename[64] = "kernel.g6a";

	char buf1[20];
	char buf2[20];

	if (argc > 1)
	{
		strcpy(filename, argv[1]);
	}

	if (command_init(filename) != 0)
		return -1;

	fprintf(stdout, "gen_kernel: wrting into file %s ...\n", filename);

	graph_startup(BLUE);

///////////////

	command_mov(1, rnum2rname(buf1, 1, ud), inum2str(buf2, 1234, ud));	// R(no1) = 1234
	command_mov(1, rnum2rname(buf1, 2, ud), inum2str(buf2, rnum2addr(3), ud));	// put the addr of R(no3) into R(no2)

	// indirect addressing
	command_mov(1, REG_ADDR, rnum2rname(buf1, 2, ud)); // addr = R(no2)

	command_mov(1, REG_INDEX, rnum2rname(buf1, 1, ud));	// store -- R[R(no2)] = R(no1);

	command_mov(1, rnum2rname(buf1, 4, ud), REG_INDEX);	// load -- R(no4) = R[R(no2)];

///////////////// verify ////////////////

	// assert(r10.04 == 1234) // R(no1)==1234
	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 04, d), "1234");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	// assert(r10.04 == r10.12) // R(no1)==R(no3)
	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 04, d), REG(r, 10, 12, d));
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	// assert(r10.08 == 0x014c) // R(no2)== addrof R(no3)
	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 08, d), "0x014c");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	// assert(r10.12 == r10.16) // R(no3)==R(no4)
	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 12, d), REG(r, 10, 16, d));
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");



///////////////
	command_mov(1, REG(r, 10, 00, d), "30");
	command_mov(1, REG(r, 10, 04, d), "31");
	command_mov(1, REG(r, 10, 08, d), "32");
	command_mov(1, REG(r, 10, 12, d), "33");
	command_mov(1, REG(r, 10, 16, d), "34");
	command_mov(1, REG(r, 10, 20, d), "35");
	command_mov(1, REG(r, 10, 24, d), "36");
	command_mov(1, REG(r, 10, 28, d), "37");

	macro_push(0, 0, 8);

	command_mov(1, REG(r, 10, 00, d), "0");
	command_mov(1, REG(r, 10, 04, d), "1");
	command_mov(1, REG(r, 10, 08, d), "2");
	command_mov(1, REG(r, 10, 12, d), "3");
	command_mov(1, REG(r, 10, 16, d), "4");
	command_mov(1, REG(r, 10, 20, d), "5");
	command_mov(1, REG(r, 10, 24, d), "6");
	command_mov(1, REG(r, 10, 28, d), "7");

	macro_pop(0, 0, 8);

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 00, d), "30");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 04, d), "31");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 08, d), "32");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 12, d), "33");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 16, d), "34");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 20, d), "35");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 24, d), "36");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	command_cmp(1, NOT_EQUAL, REG_COND, REG(r, 10, 28, d), "37");
	condition(REG_COND);
	command_jmpi(1, "NOCROSS");

	draw_point(253, 256);
	draw_point(254, 256);
	draw_point(255, 256);
	draw_point(256, 256);
	draw_point(257, 256);
	draw_point(258, 256);
	draw_point(259, 256);
	draw_point(260, 256);

	add_label("NOCROSS");
	draw_point(256, 253);
	draw_point(256, 254);
	draw_point(256, 255);
	draw_point(256, 256);
	draw_point(256, 257);
	draw_point(256, 258);
	draw_point(256, 259);
	draw_point(256, 260);

	command_terminate();

	return 0;
}
